package com.inforich.web.controller.outcomingreport;

import cn.hutool.core.collection.CollUtil;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;
import com.inforich.outcomingreport.domain.RubbishOutcomingReport;
import com.inforich.outcomingreport.domain.RubbishOutcomingReportCell;
import com.inforich.outcomingreport.service.IRubbishOutcomingReportService;

/**
 * 垃圾出厂量统计Controller
 *
 * @author liujie
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/report/outcomingreport")
public class RubbishOutcomingReportController extends BaseController {

  @Autowired
  private IRubbishOutcomingReportService rubbishOutcomingReportService;

  /**
   * 查询垃圾出厂量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:outcomingreport:list')")
  @GetMapping("/list")
  public Page<RubbishOutcomingReportCell> list(Page<RubbishOutcomingReportCell> page,
      RubbishOutcomingReport rubbishOutcomingReport) {
    Map<String, Object> map = beanToMap(rubbishOutcomingReport);
    rubbishOutcomingReportService.pageRubbishOutcomingReportCell(page, map);
    RubbishOutcomingReportCell totalCell = createTotalCell(page.getRecords());
    if (CollUtil.isNotEmpty(page.getRecords())) {
      page.getRecords().add(totalCell);
    }
    return page;
  }

  /**
   * 导出垃圾出厂量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:outcomingreport:export')")
  @Log(title = "垃圾出厂量统计", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, RubbishOutcomingReport rubbishOutcomingReport) {
    Map<String, Object> map = beanToMap(rubbishOutcomingReport);
    List<RubbishOutcomingReportCell> list = rubbishOutcomingReportService.listRubbishOutcomingReportCell(
        map);
    RubbishOutcomingReportCell totalCell = createTotalCell(list);
    list.add(totalCell);
    ExcelUtil<RubbishOutcomingReportCell> util = new ExcelUtil<RubbishOutcomingReportCell>(
        RubbishOutcomingReportCell.class);
    util.exportExcel(response, list, "垃圾出厂量统计");
  }

  /**
   * 合计
   *
   * @param cellList List<RubbishOutcomingReportCell>
   * @return RubbishOutcomingReportCell
   */
  private RubbishOutcomingReportCell createTotalCell(List<RubbishOutcomingReportCell> cellList) {
    RubbishOutcomingReportCell totalCell = new RubbishOutcomingReportCell();
    if (cellList == null || cellList.size() == 0) {
      return totalCell;
    }
    BigDecimal netWeightTotal = new BigDecimal("0");
    BigDecimal amountTotal = new BigDecimal("0");
    for (RubbishOutcomingReportCell cell : cellList) {
      netWeightTotal = netWeightTotal.add(new BigDecimal(
          Optional.ofNullable(cell.getNetWeight()).orElse("0")));
      amountTotal = amountTotal.add(
          new BigDecimal(Optional.ofNullable(cell.getAmount()).orElse("0")));
    }

    totalCell.setDisposalLandName("合计:");
    totalCell.setNetWeight(netWeightTotal.toPlainString());
    totalCell.setAmount(amountTotal.toPlainString());

    return totalCell;
  }
}
